package cn.itxc.demo.controller;

import cn.itxc.demo.datasource.DynamicDataSourceContextHolder;
import cn.itxc.demo.dto.BaseOut;
import cn.itxc.demo.service.ITestService;
import cn.itxc.demo.service.IZookeeperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Gavin on 2017/6/14.
 */
@RestController
@RequestMapping("/v1/test")
public class TestController {

    @Autowired
    private ITestService testService;

    @Autowired
    private IZookeeperService zookeeperService;


    @RequestMapping( value = "/test",method = RequestMethod.GET)
    public BaseOut test(){
        System.out.println("xxx");
        testService.testDataSource1();
        return new BaseOut();
    }

    @RequestMapping( value = "/test1",method = RequestMethod.GET)
    public BaseOut test1(){
        System.out.println("xxx");
        testService.testDataSource2();
        return new BaseOut();
    }

    @RequestMapping( value = "/test2",method = RequestMethod.GET)
    public BaseOut test2(){
        System.out.println("xxx");
        testService.testDataSource2();
        testService.testDataSource1();
        return new BaseOut();
    }


    @RequestMapping( value = "/test3",method = RequestMethod.GET)
    public BaseOut test3(){
        System.out.println("xxx -> ma");
        DynamicDataSourceContextHolder.setDataSourceType("mysql1");
        testService.testMainDataSource();
        DynamicDataSourceContextHolder.clearDataSourceType();;


        DynamicDataSourceContextHolder.setDataSourceType("mysql2");
        testService.testMainDataSource();
        DynamicDataSourceContextHolder.clearDataSourceType();;


        DynamicDataSourceContextHolder.setDataSourceType("mysql3");
        testService.testMainDataSource();
        DynamicDataSourceContextHolder.clearDataSourceType();;




//        testService.testDataSource1();
        return new BaseOut();
    }


    @RequestMapping( value = "/test4",method = RequestMethod.GET)
    public BaseOut test4(){

        zookeeperService.addNode("/zookeeper/test/mysql4","com.mysql.jdbc.Driver!192.168.90.242:3306/itxc?useSSL=false&useUnicode=true&characterEncoding=utf8!root!mindx");
        return new BaseOut();
    }
}
